package com.example.dolgi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Item2 extends Activity {
	
	private static final int CM_DELETE_ID = 1;
	final String ATTRIBUTE_NAME_TEXT = "text";
	final String ATTRIBUTE_NAME_IMAGE = "image";
	ListView lvSimple;
	SimpleAdapter sAdapter;
	ArrayList<Map<String, Object>> data;
	Map<String, Object> m;
	EditText etName,etDebt;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item2);
		data = new ArrayList<Map<String, Object>>();
		String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE };
		int[] to = { R.id.tvText, R.id.ivImg };
		sAdapter = new SimpleAdapter(this, data, R.layout.item2add, from, to);
		lvSimple = (ListView) findViewById(R.id.lvSimple);
		lvSimple.setAdapter(sAdapter);
		registerForContextMenu(lvSimple);
		etName = (EditText) findViewById(R.id.etName);
		etDebt = (EditText) findViewById(R.id.etDebt);
	}
	public void onButtonClick(View v) {
		m = new HashMap<String, Object>();
		m.put(ATTRIBUTE_NAME_TEXT, etName.getText().toString());
		m.put(ATTRIBUTE_NAME_IMAGE, etDebt.getText().toString());
		data.add(m);
		sAdapter.notifyDataSetChanged();
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		Resources r = getResources();
		String value = r.getString(R.string.contextMenu);
		menu.add(0, CM_DELETE_ID, 0, value);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() == CM_DELETE_ID) {
			AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
			data.remove(acmi.position);
			sAdapter.notifyDataSetChanged();
			return true;
		}
		return super.onContextItemSelected(item);
	}
}
